//
//  SettingCell.m
//  QQplayground
//
//  Created by zhangxy on 2018/5/22.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "SettingCell.h"
#import <UMPush/UMessage.h>
#import "AppDelegate.h"

@implementation SettingCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [_switchView setTintColor:UIColorFromRGB(BgTitleColor)];
    [_switchView setOnTintColor:UIColorFromRGB(BgTitleColor)];
    
    [_labTitle attributeFont:FontFiftent textColor:UIColorFromRGB(TextSizeOneColor)];
    
    _labLine.hidden = YES;
    _labLine.backgroundColor = LINE_COLOR;
    
    [_labDesc attributeFont:FontFiftent textColor:UIColorFromRGB(TextSizeOneColor)];
    
    [_switchView addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
}

-(void)changeSwitch:(UISwitch *) sw{
    if(sw.isOn){
        // Push组件基本功能配置
        UMessageRegisterEntity * entity = [[UMessageRegisterEntity alloc] init];
        //type是对推送的几个参数的选择，可以选择一个或者多个。默认是三个全部打开，即：声音，弹窗，角标
        entity.types = UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionSound|UMessageAuthorizationOptionAlert;
        
        
        [UMessage registerForRemoteNotificationsWithLaunchOptions:nil Entity:entity completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
            }else{
            }
        }];
        
        [UMessage registerDeviceToken:((AppDelegate *)[UIApplication sharedApplication].delegate).token];
    }else{
        [UMessage unregisterForRemoteNotifications];
    }
}

- (BOOL)isAllowedNotification
{
    //iOS8 check if user allow notification
    UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
    if(UIUserNotificationTypeNone != setting.types) {
        return YES;
    }
    return  NO;
}

- (void)toSystemNotifictionPage
{
    //ios8 to system notification page
    UIApplication *application = [UIApplication sharedApplication];
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([application canOpenURL:url]) {
        if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
            [application openURL:url options:@{} completionHandler:nil];
        }else{
            [application openURL:url];
        }
    }
}

-(void)dataToView:(NSDictionary *)item{
    [_labTitle setText:convertToString(item[@"title"])];
    
    int code = [item[@"code"] intValue];
    _switchView.hidden = YES;
    _labDesc.hidden = YES;
    _imgIcon.hidden = NO;
    _switchView.enabled = NO;
    if(code == 4){
        _switchView.hidden = YES;
        _imgIcon.hidden = NO;
        _labDesc.hidden = NO;
        
        if([self isAllowedNotification]){
            [_switchView setOn:YES];
            [_labDesc setText:@"推送已开启"];
            
            [_labDesc removeAllGestureRecognizer];
            
        }else{
            [_switchView setOn:NO];

            [_labDesc setText:@"请在iPhone的“设置-通知”允许推送"];
            [_labDesc addTapGestureRecognizerWithTarget:self action:@selector(toSystemNotifictionPage)];
        }
    }
    if(convertToString(item[@"value"]).length > 0){
        _labDesc.hidden = NO;
        [_labDesc setText:convertToString(item[@"value"])];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
